package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        model.addAttribute("result",strategyDetailService.selectForList(qo));

        List<Strategy> list = strategyService.selectAll();
        model.addAttribute("strategies", list);
        return "strategyDetail/list";
    }

    @RequestMapping("getCatalogBystrategyId")
    @ResponseBody
    public Object getCatalogBystrategyId(Long strategyId){
        return strategyDetailService.getCatalogBystrategyId(strategyId);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(StrategyDetail strategyDetail, MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                //String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PAGE + "upload");
                String  imgUrl= SevenUploadUtil.uploadQiniu(file);
                strategyDetail.setCoverUrl(imgUrl);
            }
            strategyDetailService.saveOrUpdate(strategyDetail);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @RequestMapping("getContentByStrategyId")
    @ResponseBody
    public Object getContentByStrategyId(Long detailId){
            return strategyDetailService.getContentByStrategyId(detailId);
    }
}
